package cn.tedu.controller;

import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import cn.tedu.vo.PageResult;
import cn.tedu.vo.SysResult;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        String token = userService.selectUserByUP(user);
        if(token == null || "".equals(token)){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }
    @GetMapping("/list")
    public SysResult getUserListByPage(PageResult pageResult){
        pageResult = userService.getUserListByPage(pageResult);
        return SysResult.success(pageResult);
    }
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatusById(User user){
        userService.updateStatusById(user);
        return SysResult.success();
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }
    @GetMapping("/{id}")
    public SysResult findUserById(@PathVariable Integer id){
        User user = userService.findUserById(id);
        return SysResult.success(user);
    }
    @PutMapping("/updateUser")
    public SysResult updateUserById(@RequestBody User user){
        userService.updateUserById(user);
        return SysResult.success();
    }
    @DeleteMapping("/{id}")
    public SysResult deleteUserById(@PathVariable Integer id){
        userService.deleteUserById(id);
        return SysResult.success();
    }
    @RequestMapping("/test")
    public String Tests(){
        return "测试成功";
    }
}
